package net.bezrabotnih.cabinet.client.ui;

import java.util.Date;

import net.bezrabotnih.client.managed.request.JobProxy;
import net.bezrabotnih.client.managed.request.UserAccountProxy;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class PerformerJobBlockView extends VerticalPanel{
	
	public interface Presenter {
		
		
		void jobDetailsClicked(String jobId);
		
		void applyTo(String jobId);
		
		void cancelApplication(String jobId);
	
		
	}
	
	private Presenter listener;
	
	private boolean applied;
	
	private Button applicationButton;
	
	final VerticalPanel infoPanel;
	
	public PerformerJobBlockView(Presenter listener,final String jobId,String jobName,String jobType,String providerId,boolean isHourly,Date startDate, Date deadline, Integer price,String description,boolean applied,boolean winner) {
		
        this.listener=listener;
        
        this.applied=applied;
		
		
		DisclosurePanel disclosurePanel=new DisclosurePanel("Подробнее");
//		disclosurePanel.setHeader(new HTML(jobProxy.getName(),true));
		disclosurePanel.setAnimationEnabled(true);
		
		VerticalPanel panel=new VerticalPanel();
		String isHourlyStr="";
		
		if (isHourly) isHourlyStr=" почасовая";
		HTML jobTypeHTML=new HTML("<b>Тип работы:</b>: "+jobType+" &nbsp;&nbsp;&nbsp;"+isHourlyStr+"<br><br>",true);
		jobTypeHTML.setHorizontalAlignment(HasAlignment.ALIGN_RIGHT);
		
		if (price!=null) panel.add(new HTML("<b>Цена:</b> "+price+" руб.<br><br>",true));
		if (startDate!=null)  panel.add(new HTML("<b>Начало:</b>: "+startDate+" <br><br>",true));
		if (deadline!=null)  panel.add(new HTML("<b>Последний срок:</b>: "+deadline+" <br><br>",true));
		if (description!=null&&!description.equals("")) panel.add(new HTML("<b>Описание</b>: "+description+"<br><br><br>",true));
		infoPanel=new VerticalPanel();
		panel.add(infoPanel);
		
		
		
		disclosurePanel.addOpenHandler(new OpenHandler<DisclosurePanel>() {
			
			@Override
			public void onOpen(OpenEvent<DisclosurePanel> event) {

				if (infoPanel.getWidgetCount()==0){
					PerformerJobBlockView.this.listener.jobDetailsClicked(jobId);
				}
				
			}
			
		});
		
		PerformerJobBlockView.this.applicationButton=new Button();
		
		if (!winner){
	
		String applicationStr=applied?"Отменить заявку":"Подать заявку";
		
		PerformerJobBlockView.this.applicationButton.setText(applicationStr);
		PerformerJobBlockView.this.applicationButton.addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent event) {
				PerformerJobBlockView.this.applicationButton.setEnabled(false);

				if (!PerformerJobBlockView.this.applied) {
					PerformerJobBlockView.this.listener.applyTo(jobId);
	
				}else{
					PerformerJobBlockView.this.listener.cancelApplication(jobId);
					
				}
				
			}
		});
		
		panel.add(PerformerJobBlockView.this.applicationButton);
		
		}
		
		disclosurePanel.setContent(panel);
		
		
		HorizontalPanel titlePanel=new HorizontalPanel();
		titlePanel.setWidth("370px");
		titlePanel.add(new HTML("<b>"+jobName+"</b>",true));
		titlePanel.add(jobTypeHTML);
		
		VerticalPanel verticalPanel=new VerticalPanel();
		verticalPanel.setWidth("370px");
		verticalPanel.add(titlePanel);
		verticalPanel.setSpacing(4);
		verticalPanel.add(disclosurePanel);
		
	
		this.setBorderWidth(1);
		this.add(verticalPanel);
				
		
	}
	
	public void setApplicationSuccessView() {
		applicationButton.setEnabled(true);
		applicationButton.setText("Отменить заявку");
		applied=true;
		System.out.println("ApplicationSuccessView");
	}




	public void setApplicationFailureView() {
		Window.alert("Ошибка сервера. Не удалось подать заявку. Попробуйте еще раз.");
		applicationButton.setText("Подать заявку");
		applied=false;
		applicationButton.setEnabled(true);
		System.out.println("ApplicationFailureView");
		
	}




	public void setApplicationCancelSuccessView() {
		applicationButton.setEnabled(true);
		applicationButton.setText("Подать заявку");
		applied=false;
		System.out.println("ApplicationCancelSuccessView");
		
	}




	public void setApplicationCancelFailureView() {
		Window.alert("Ошибка сервера. Не удалось отменить заявку. Попробуйте еще раз.");
		applicationButton.setText("Отменить заявку");
		applied=true;
		applicationButton.setEnabled(true);
		System.out.println("ApplicationCancelFailureView");
		
	}
	
	
	public void setProviderName(String name) {
		if (infoPanel!=null) infoPanel.add(new HTML("<b>Работодатель:</b> "+name+"<br><br><br>",true));
		
	}

}
